/*************************************************************************
 * File Name:    Minimum_Depth_of_Binary_Tree.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013/11/4 11:28:33
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem:Minimum Depth of Binary Tree
 |
 | Given a binary tree, find its minimum depth.
 |
 | The minimum depth is the number of nodes along the shortest path from
 | the root node down to the nearest leaf node.
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode *root)
    {
        if (root == NULL) return 0;
        
        queue<pair<TreeNode *, int> > Q;
        pair<TreeNode *, int> p;
        
        Q.push(make_pair(root, 1));
        while (!Q.empty()) {
            p = Q.front(), Q.pop();
            
            if (p.first->left == NULL && p.first->right == NULL) {
                return p.second;
            }
            if (p.first->left != NULL) {
                Q.push(make_pair(p.first->left, p.second + 1));
            }
            if (p.first->right != NULL) {
                Q.push(make_pair(p.first->right, p.second + 1));
            }
        }
        return 0;
    }
};

